package com.example.mbl.service.impl;

import ch.qos.logback.core.joran.action.NewRuleAction;
import com.example.mbl.dao.NewsMapper;
import com.example.mbl.entity.News;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.NewsPageResult;
import com.example.mbl.result.business.NewsResult;
import com.example.mbl.service.NewsService;
import com.example.mbl.util.Tools;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public List<News> searchItemsByKeyWord(String keyword) {
        return newsMapper.searchItemsByKeyWord(keyword);
    }

    @Override
    public WebRestResult addNews(News news) {
        WebRestResult result = new WebRestResult();
        news.setNewsid(UUIDUtil.randomUUID());
        if(Tools.isEmpty(news.getFlag()))
            news.setFlag("0");
        news.setCreatedate(new Date());
        if(news.getTopflag().equals("1") && !Tools.isEmpty(newsMapper.findTopNews())){
            news.setTopflag("0");
        }
        if(newsMapper.insert(news)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增新闻失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteNews(News news) {
        WebRestResult result = new WebRestResult();
        if(newsMapper.deleteByPrimaryKey(news.getNewsid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除新闻失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateNews(News news) {
        WebRestResult result = new WebRestResult();
        if(!Tools.isEmpty(news.getTopflag()) && news.getTopflag().equals("1")){
            String newid= newsMapper.findTopNews();
            if (!Tools.isEmpty(newid)) {
                if (newid.equals(news.getNewsid())) {
                    if(newsMapper.updateByPrimaryKeySelective(news)==1){
                        result.setResult(WebRestResult.SUCCESS);
                        result.setMessage("该新闻已是置顶新闻，修改新闻内容成功");
                        return result;
                    }
                } else {
                    result.setResult(WebRestResult.FAILURE);
                    result.setMessage("已有置顶新闻");
                    return result;
                }
            }
        }
        if(newsMapper.updateByPrimaryKeySelective(news)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新新闻失败");
        }
        return result;
    }

    @Override
    public WebRestResult setTopNews(News news) {
        WebRestResult result = new WebRestResult();
        if(!Tools.isEmpty(newsMapper.findTopNews())){
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("已有置顶新闻");
        }
        if(newsMapper.setTopNews(news.getNewsid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("置顶新闻失败");
        }
        return result;
    }

    @Override
    public PageResult<NewsPageResult> getNewsList(String newstype, String newsdate, String newstitle, int page, int pagesize) {
        PageResult<NewsPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;
        List<NewsPageResult> list = newsMapper.getNewsList(newstype,newsdate,newstitle,start,pagesize);
        if(list.size()>0){
            result.setPageCount(list.get(0).getPage());
            result.setTotal(list.get(0).getCnt());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public NewsResult getNewsItem(String newsid) {
        NewsResult result = newsMapper.getNewsItem(newsid);
        if(!Tools.isEmpty(result)){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取新闻详情失败");
        }
        return result;
    }

    @Override
    public WebRestResult setNewsVisible(News news) {
        WebRestResult result = new WebRestResult();
        News tnews = newsMapper.selectByPrimaryKey(news.getNewsid());
        if(tnews.getFlag().equals("0"))
            tnews.setFlag("1");
        else
            tnews.setFlag("0");
        if(newsMapper.updateByPrimaryKeySelective(tnews)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("设置失败");
        }
        return result;
    }
}
